<?php
// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2018~2018 GoMaoMao [ http://www.gomaomao.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.gomaomao.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | 联系方式：GoMaoMao <vip.vvm@qq.com>
// +----------------------------------------------------------------------
// | github开源项目：https://gitee.com/gomaomao.com/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\miniprogram\controller;

use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use think\Db;
use think\facade\Log;

/**
 * 文章分类管理
 * Class Cate
 * @package app\store\controller
 * @author Anyon <zoujingli@qq.com>
 * @date 2017/03/27 14:43
 */
class ProductCate extends BasicAdmin
{

	/**
	 * 定义当前操作表名
	 * @var string
	 */
	public $table = 'mp_product_cate';

	function initialize()
	{
		// 设置website
		$website = session('website');
		if(empty($website)){
			$user_id = session('user.id');
			$website = Db::name('mp_website')->where('user_id = ' . $user_id)->limit(0,2)->select();
			if(count($website)==1){
				session('website',$website[0]['id']);
			}
		}
		if(empty(session('website'))){
			list($base, $spm, $url) = [url('@admin'), $this->request->get('spm'), url('miniprogram/index/index')];
			$this->error('请先选择站点!',"{$base}#{$url}?spm={$spm}");
		}
	}

	/**
	 * 分类列表
	 * @return array|string
	 * @throws \think\Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function index()
	{
		$this->title = '分类管理';
		$model       = Db::name($this->table);
		$model->where('website', session('website'));
		$db = $model->where('is_deleted', '0');
		return parent::_list($db->order('sort asc,id asc'), false);
	}

	/**
	 * 列表数据处理
	 * @param array $data
	 */
	protected function _index_data_filter(&$data)
	{
		foreach ($data as &$vo) {
			$vo['ids'] = join(',', ToolsService::getArrSubIds($data, $vo['id']));
		}
		$data = ToolsService::arr2table($data);
	}

	/**
	 * 添加菜单
	 * @return array|string
	 * @throws \think\Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function add()
	{
		return $this->_form($this->table, 'form');
	}

	/**
	 * 编辑菜单
	 * @return array|string
	 * @throws \think\Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function edit()
	{
		return $this->_form($this->table, 'form');
	}

	/**
	 * 表单数据前缀方法
	 * @param array $vo
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	protected function _form_filter(&$vo)
	{
		if ($this->request->isGet()) {
// 读取上级分类
			$where  = ['status' => '1', 'is_deleted' => '0'];
			$_cates = (array)Db::name($this->table)->where($where)->order('sort desc,id desc')->select();
			array_unshift($_cates, ['id' => 0, 'pid' => -1, 'cate_title' => '--- 顶级分类 ---']);
			$cates = ToolsService::arr2table($_cates);
			foreach ($cates as $key => &$cate) {
				if (isset($vo['pid'])) {
					$path = "-{$vo['pid']}-{$vo['id']}";
					if ($vo['pid'] !== '' && (stripos("{$cate['path']}-", "{$path}-") !== false || $cate['path'] === $path)) {
						unset($cates[$key]);
					}
				}
			}
			$this->assign('cates', $cates);
		}
		$vo['website'] = session('website');
	}

	/**
	 * 删除分类
	 * @throws \think\Exception
	 * @throws \think\exception\PDOException
	 */
	public function del()
	{
		if (DataService::update($this->table)) {
			$this->success("分类删除成功！", '');
		}
		$this->error("分类删除失败，请稍候再试！");
	}

	/**
	 * 分类禁用
	 * @throws \think\Exception
	 * @throws \think\exception\PDOException
	 */
	public function forbid()
	{
		if (DataService::update($this->table)) {
			$this->success("分类禁用成功！", '');
		}
		$this->error("分类禁用失败，请稍候再试！");
	}

	/**
	 * 分类禁用
	 * @throws \think\Exception
	 * @throws \think\exception\PDOException
	 */
	public function resume()
	{
		if (DataService::update($this->table)) {
			$this->success("分类启用成功！", '');
		}
		$this->error("分类启用失败，请稍候再试！");
	}

}
